package com.example.yt_app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yt_app.R;

import java.util.ArrayList;
import java.util.Map;

/**
 *
 * @author dell
 * @date 2021/7/22
 */
@SuppressWarnings("ALL")
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    protected Context context;
    ArrayList<Map<String, Object>> list;
    /**
     * 首先在adapter中自定义一个接口， 然后在onBindViewHolder中去为holder.itemView去设置相应
     的监听最后回调我们设置的监听。
     */
    protected onItemClickListener onItemClickListener;

    public RecyclerViewAdapter(Context context, ArrayList<Map<String, Object>> list) {
        this.context = context;
        this.list = list;
    }

    public void setOnItemClickListener(RecyclerViewAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = (RecyclerViewAdapter.onItemClickListener) onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.e("aaa", "Myadapter--onCreateViewHolder--parent" + parent.toString());
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.simple_adapter_item, parent, false));
        return holder;
    }

    @SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaRemoveCommentedCode"})
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.id.setText(list.get(position).get("id").toString());
        holder.name.setText(list.get(position).get("name").toString());
        holder.online.setText(list.get(position).get("online").toString());
        //noinspection AlibabaUndefineMagicConstant,AlibabaUndefineMagicConstant
        if (list.get(position).get("online").toString() == "true"){
            //String o = String.valueOf(devices.get(i).getOnline());
            holder.online.setTextColor(Color.GREEN);
        }else {
            holder.online.setTextColor(Color.RED);
        }


        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    public void Remove(int position){
        list.remove(position);
        notifyItemRemoved(position);
    }

    @SuppressWarnings("AlibabaRemoveCommentedCode")
    public void addData(int position){
        //list.add(position,"刚增加的");
        notifyItemInserted(position);
    }


    @SuppressWarnings({"AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc", "AlibabaClassNamingShouldBeCamel"})
    public interface onItemClickListener {

        @SuppressWarnings("AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc")
        void onItemClick(View view, int position);

        @SuppressWarnings("AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc")
        void onItemLongClick(View view, int position);
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView id;
        public TextView name;
        public TextView online;
        public CardView cv;

        public ViewHolder(View itemView) {
            super(itemView);
            id = (TextView)itemView.findViewById(R.id.sai_id);
            name = (TextView)itemView.findViewById(R.id.sai_name);
            online = (TextView)itemView.findViewById(R.id.sai_phone);
            cv = itemView.findViewById(R.id.cv);
        }

    }

}
